package wifi_locator_client.just.wzy.wifi_locator_client;

import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzy on 15-4-11.
 * 定位界面
 */
public class LocateActivity extends ActionBarActivity {
    private Button locate_openWifi;
    private Button locate_closeWifi;
    private Button startLocte;
    private WifiController wifiController;
    private List<ScanResult> scanResults;
    private List<String> filtrateTable;
    private EditText portNumberLocate;
    private EditText ipAddressLocate;
    private TransmissionManager transmissionManager;
    private TextView locationText;

    //静态handler，用以监听外部类动态，从而更新activity
    static Handler locateHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locate_layout);

        wifiController = new WifiController(LocateActivity.this);
        filtrateInit();
        init();
    }

    /**
     * 控件初始化
     */
    private void init()
    {
        locate_openWifi = (Button) findViewById(R.id.locate_openWifi);
        locate_closeWifi = (Button) findViewById(R.id.locate_closeWifi);
        startLocte = (Button) findViewById(R.id.startLocate);
        portNumberLocate = (EditText) findViewById(R.id.locate_portNumber);
        ipAddressLocate = (EditText) findViewById(R.id.locate_ipAddress);
        locationText = (TextView) findViewById(R.id.locationText);
        locate_openWifi.setOnClickListener(new MyListener());
        locate_closeWifi.setOnClickListener(new MyListener());
        startLocte.setOnClickListener(new MyListener());
    }

    /**
     * 筛选器初始化
     */
    private void filtrateInit() {
        filtrateTable = new ArrayList<>();
        String str[] = {"ChinaUnicom", "CU_Campus", "CMCC-EDU", "ChinaNet-EDU", "CMCC-JDK"};
        for (String x : str) {
            filtrateTable.add(x);
        }
    }

    /**
     * 筛选器
     * @param scanResults 搜寻到的热点信息
     */
    private void filtrator(List<ScanResult> scanResults) {
        for (int i = 0; i < scanResults.size(); i++) {
            if (!filtrateTable.contains(scanResults.get(i).SSID)) {
                scanResults.remove(i);
                i--;
            }
        }
    }

    /**
     * 获取所有AP的相关信息
     */
    void getAllWifiList() {
        if (scanResults != null)
            scanResults.clear();
        scanResults = wifiController.scanWifi();
        filtrator(scanResults);
    }

    /**
     * 内部类，空间监听
     */
    class MyListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId())
            {
                case R.id.locate_openWifi:
                    wifiController.openWifi();
                    Toast.makeText(LocateActivity.this, "WIFI已打开！可以开始定位！", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.locate_closeWifi:
                    wifiController.closeWifi();
                    Toast.makeText(LocateActivity.this, "WIFI已关闭！", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.startLocate:
                    getAllWifiList();
                    String host = ipAddressLocate.getText().toString();
                    int port = Integer.parseInt(portNumberLocate.getText().toString());
                    transmissionManager = new TransmissionManager(host, port, LocateActivity.this, scanResults);
                    transmissionManager.sendMessage(1);
                    //transmissionManager.receiveMessage(1);



                    locateHandler = new Handler(){
                        @Override
                        public void handleMessage(Message msg) {
                            super.handleMessage(msg);
                            String location = transmissionManager.getLocation();
                            locationText.setText(location);
                        }
                    };


            }
        }
    }
}
